<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      /**
       * @param {number[][]} mat
       * @return {number[]}
       */
      var findDiagonalOrder = function (mat) {
        let xlen = mat[0].length;
        let ylen = mat.length;
        let arr = new Array(xlen * ylen);
        let x = 0;
        let y = 0;
        for (let i = 0; i < arr.length; i++) {
          arr[i] = mat[y][x];
          if ((x + y) % 2 === 0) {
            if (x === xlen - 1) {
              y++;
            } else if (y === 0) {
              x++;
            } else {
              x++;
              y--;
            }
          } else if ((x + y) % 2 === 1) {
            if (y === ylen - 1) {
              x++;
            } else if (x === 0) {
              y++;
            } else {
              x--;
              y++;
            }
          }
        }

        return arr;
      };
      let data = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
      ];
      let res = findDiagonalOrder(data);
      console.log(JSON.stringify(data));
      console.log(res);
      console.log([1,2,4,7,5,3,6,8,9]);
    </script>
  </body>
</html>
